package com.example.delll.kugou.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.delll.kugou.R;
import com.example.delll.kugou.adapter.MyViewPaper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tab_layout;
    private ViewPager vp_show;
    private List<View> myViewPaperList;
    private LayoutInflater inflater;
    private MyViewPaper myViewPaper;
    private List<String> myTitleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intoView();
        intoViewPaper();
    }

    /**
     * 为 ViewPaper  初始化和添加数据 并绑定 适配器
     */
    private void intoViewPaper() {
        myViewPaperList = new ArrayList<>();
        myTitleList = new ArrayList<>();
        inflater = LayoutInflater.from(this);
        myViewPaperList.add(inflater.inflate(R.layout.view_paper_my,null));
        myViewPaperList.add(inflater.inflate(R.layout.view_paper_listen,null));
        myViewPaperList.add(inflater.inflate(R.layout.view_paper_look,null));
        myViewPaperList.add(inflater.inflate(R.layout.view_paper_sing,null));
        myTitleList.add("我");
        myTitleList.add("听");
        myTitleList.add("看");
        myTitleList.add("唱");
        intoTabLayout();
        myViewPaper = new MyViewPaper(myViewPaperList,myTitleList);
        vp_show.setAdapter(myViewPaper);
        tab_layout.setupWithViewPager(vp_show);
        tab_layout.getTabAt(1).select();
    }

    /**
     * 为 TabLayout 添加  标题
     */
    private void intoTabLayout() {
        tab_layout.addTab(tab_layout.newTab().setText(myTitleList.get(0)));
        tab_layout.addTab(tab_layout.newTab().setText(myTitleList.get(1)));
        tab_layout.addTab(tab_layout.newTab().setText(myTitleList.get(2)));
        tab_layout.addTab(tab_layout.newTab().setText(myTitleList.get(3)));
    }

    /**
     * 初始化view
     */
    private void intoView() {
        tab_layout = findViewById(R.id.tab_layout);
        vp_show = findViewById(R.id.vp_show);
    }


}
